/***************************************************************************
 * This routine is taken from the book "The Data Compression Book 2nd edition" 
 * I corrected some printing error and added the routine to clean up the tree
 *                                                    modified by nalsas dai
 ***************************************************************************/
/******************** Start of BITIO.H **********************/ 
 
 
class tIobuf
{   public:  
      char* buf;//buffer
      long len;//buffer length     
      long curpos; 
      ~tIobuf() {if (buf) delete[] buf; }
      tIobuf()
      {
       len=curpos=0;
       buf=NULL;
      };
      inline int isEof()
      {if(curpos>len)
       return 1;
       else return 0;
      };
      inline int getbyte()
      {
       if(buf)
        if(!isEof())
         return buf[curpos++];else return -10000;
       else {printf("tIobuf buf wrong!");return -10000;}
      };
      inline int putbyte(int inchar)
      {if(!isEof())
        {buf[curpos++]=inchar;
         return 1;
         }
       else return 0;
      };

      inline void close()
      {
      };
};
 
typedef struct bit_file { 
     unsigned char mask; 
     int rack; 
     int pacifier_counter;
     tIobuf *file; 
} BIT_FILE; 
 
BIT_FILE*     OpenInputBitFile(tIobuf *ifile); 
BIT_FILE*     OpenOutputBitFile(tIobuf *ofile); 
void          OutputBit( BIT_FILE *bit_file, int bit ); 
void          OutputBits( BIT_FILE *bit_file,  unsigned long code, int count ); 
int           InputBit( BIT_FILE *bit_file ); 
unsigned long InputBits( BIT_FILE *bit_file, int bit_count ); 
void          CloseInputBitFile( BIT_FILE *bit_file ); 
void          CloseOutputBitFile( BIT_FILE *bit_file ); 
void          FilePrintBinary( FILE *file, unsigned int code, int bits); 

 
/********************** End of BITIO.H *********************/ 
